﻿
namespace NetROS.Tiers.Windows.Forms
{
	/// <summary>
	/// abstract IspisKolona typed datagridview
	/// </summary>
	[System.ComponentModel.DesignerCategoryAttribute("code")]
	public abstract class IspisKolonaDataGridViewBase : System.Windows.Forms.UserControl
	{	
		/// <summary> 
		/// Thrown when the current Entity changed.
		/// </summary>
		public event System.EventHandler<IspisKolonaDataGridViewEventArgs> CurrentEntityChanged;
		
		#region Fields
				
		/// <summary> 
		/// the current entity.
		/// </summary>
		private Entities.IspisKolona _currentIspisKolona = null;
		//private Dictionary<string, string> _columnsHeaders = new Dictionary<string, string>();
		
		/// <summary> 
		/// the datagridview instance
		/// </summary>
		protected System.Windows.Forms.DataGridView uxIspisKolonaDataGridView;
		
		/// <summary> 
		/// the errorprovider
		/// </summary>
		protected System.Windows.Forms.ErrorProvider uxIspisKolonaErrorProvider;
		
		/// <summary> 
		/// the bindingsource
		/// </summary>
		protected System.Windows.Forms.BindingSource uxIspisKolonaBindingSource;
		
		
		/// <summary> 
		/// the DGV column associated with the ID property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxIDDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the Naziv property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxNazivDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the Naziv2 property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxNaziv2DataGridViewColumn;
		/// <summary> 
		/// the DGV column associated with the IDIspis property
		/// </summary>
		protected System.Windows.Forms.DataGridViewComboBoxColumn uxIDIspisDataGridViewColumn;
		
		#endregion
					
		#region Combo list
		
				
		private Entities.TList<Entities.Ispis> _IDIspisList;
		
		/// <summary> 
		/// The list of selectable Ispis
		/// </summary>
		[System.ComponentModel.Category("DropDownList BindingSources")]
		public Entities.TList<Entities.Ispis> IDIspisList
		{
			get {return this._IDIspisList;}
			set 
			{
				this._IDIspisList = value;
				this.uxIDIspisDataGridViewColumn.DataSource = null;
				this.uxIDIspisDataGridViewColumn.DataSource = this._IDIspisList;
			}
		}
		
		private bool _allowNewItemInIDIspisList;
		
		/// <summary> 
		/// Indicates if user can add an item in the list of Ispis
		/// </summary>
		[System.ComponentModel.Category(".netTiers"), System.ComponentModel.Description("Indicates if end user can add a new item in the combobox. an event will be thrown then.")]
		public bool AllowNewItemInIDIspisList
		{
			get { return _allowNewItemInIDIspisList;}
			set
			{
				this._allowNewItemInIDIspisList = value;
				this.uxIDIspisDataGridViewColumn.DisplayStyle = value ? System.Windows.Forms.DataGridViewComboBoxDisplayStyle.ComboBox : System.Windows.Forms.DataGridViewComboBoxDisplayStyle.DropDownButton;
			}
		}
		
		#endregion
		
		#region Main Datasource
		
		private Entities.TList<Entities.IspisKolona> _IspisKolonaList;
				
		/// <summary> 
		/// The list of IspisKolona to be displayed in the DGV.
		/// </summary>
		[System.ComponentModel.Category("Data")]
		public Entities.TList<Entities.IspisKolona> IspisKolonaList
		{
			get {return this._IspisKolonaList;}
			set
			{
				this._IspisKolonaList = value;
				this.uxIspisKolonaBindingSource.DataSource = null;
				this.uxIspisKolonaBindingSource.DataSource = value;
				this.uxIspisKolonaDataGridView.DataSource = null;
				this.uxIspisKolonaDataGridView.DataSource = this.uxIspisKolonaBindingSource;				
				//this.uxIspisKolonaBindingSource.ListChanged += new System.ComponentModel.ListChangedEventHandler(uxIspisKolonaBindingSource_ListChanged);
				this.uxIspisKolonaBindingSource.CurrentItemChanged += new System.EventHandler(OnIspisKolonaBindingSourceCurrentItemChanged);
			}
		}
		
		private void OnIspisKolonaBindingSourceCurrentItemChanged(object sender, System.EventArgs e)
		{
			_currentIspisKolona = uxIspisKolonaBindingSource.Current as Entities.IspisKolona;
			
			if (_currentIspisKolona != null)
			{
				_currentIspisKolona.Validate();
			}
			//_IspisKolona.Validate();
			OnCurrentEntityChanged();
		}

		//void uxIspisKolonaBindingSource_ListChanged(object sender, System.ComponentModel.ListChangedEventArgs e)
		//{
			//throw new Exception("The method or operation is not implemented.");
		//}		
		
		#endregion
		
		/// <summary>
		/// Gets the currently selected <c cref="Entities.IspisKolona"/> instance.
		/// </summary>
		public Entities.IspisKolona SelectedIspisKolona
		{
			get {return this._currentIspisKolona;}
		}
		
		#region other properties
		
		/// <summary>
		/// Gets or sets a value indicating whether you have provided your own data-management operations for the DataGridView control.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool VirtualMode
		{
			get {return this.uxIspisKolonaDataGridView.VirtualMode;}
			set
			{
				this.uxIspisKolonaDataGridView.VirtualMode = value;
			}
		}
		
		/// <summary>
		/// Gets or sets a value indicating whether the option to add rows is displayed to the user.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool AllowUserToAddRows
		{
			get {return this.uxIspisKolonaDataGridView.AllowUserToAddRows;}
			set {this.uxIspisKolonaDataGridView.AllowUserToAddRows = value;}
		}
		
		/// <summary>
		/// Gets or sets a value indicating whether the user is allowed to delete rows from the DataGridView.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool AllowUserToDeleteRows
		{
			get {return this.uxIspisKolonaDataGridView.AllowUserToDeleteRows;}
			set {this.uxIspisKolonaDataGridView.AllowUserToDeleteRows = value;}
		}
		
		/// <summary>
		/// Gets the <c cref="DataGridView"/> instance.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public System.Windows.Forms.DataGridView DataGridView
		{
			get {return this.uxIspisKolonaDataGridView;}
		}
		
		/// <summary>
		/// Gets a collection that contains all the columns in the control.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]		
		[System.ComponentModel.DesignerSerializationVisibilityAttribute(System.ComponentModel.DesignerSerializationVisibility.Visible)]
		//[System.ComponentModel.EditorAttribute("System.Windows.Forms.Design.DataGridViewColumnCollectionEditor, System.Design", typeof(System.Drawing.Design.UITypeEditor))] 
		public System.Windows.Forms.DataGridViewColumnCollection Columns
		{
			get { return this.uxIspisKolonaDataGridView.Columns; }
		}
		
		#endregion
		
		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.IContainer components = null;
	
		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
		protected override void Dispose(bool disposing)
		{
			if (disposing && (components != null))
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}
	
		#region Constructor
	
		/// <summary>
		/// Initializes a new instance of the <see cref="IspisKolonaDataGridViewBase"/> class.
		/// </summary>
		public IspisKolonaDataGridViewBase()
		{			
			this.components = new System.ComponentModel.Container();			
			
			this.uxIspisKolonaDataGridView = new System.Windows.Forms.DataGridView();
			this.uxIspisKolonaBindingSource = new System.Windows.Forms.BindingSource(this.components);
			this.uxIspisKolonaErrorProvider = new System.Windows.Forms.ErrorProvider(this.components);
			this.uxIDDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxNazivDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxNaziv2DataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxIDIspisDataGridViewColumn = new System.Windows.Forms.DataGridViewComboBoxColumn();
			//this.uxIDIspisBindingSource = new IspisBindingSource(this.components);
			//((System.ComponentModel.ISupportInitialize)(this.uxIDIspisBindingSource)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxIspisKolonaDataGridView)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxIspisKolonaBindingSource)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxIspisKolonaErrorProvider)).BeginInit();
			this.SuspendLayout();
						
			// 
			// uxIspisKolonaErrorProvider
			// 
			this.uxIspisKolonaErrorProvider.ContainerControl = this;
			this.uxIspisKolonaErrorProvider.DataSource = this.uxIspisKolonaBindingSource;						
			// 
			// uxIspisKolonaDataGridView
			// 
			this.uxIspisKolonaDataGridView.AutoGenerateColumns = false;
			this.uxIspisKolonaDataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
			this.uxIspisKolonaDataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
		this.uxIDDataGridViewColumn,
		this.uxNazivDataGridViewColumn,
		this.uxNaziv2DataGridViewColumn,
		this.uxIDIspisDataGridViewColumn			});
			this.uxIspisKolonaDataGridView.Dock = System.Windows.Forms.DockStyle.Fill;
			this.uxIspisKolonaDataGridView.Location = new System.Drawing.Point(0, 0);
			this.uxIspisKolonaDataGridView.Name = "uxIspisKolonaDataGridView";
			this.uxIspisKolonaDataGridView.Size = new System.Drawing.Size(470, 300);
			this.uxIspisKolonaDataGridView.TabIndex = 0;	
			this.uxIspisKolonaDataGridView.BackgroundColor = System.Drawing.Color.WhiteSmoke;
			this.uxIspisKolonaDataGridView.EnableHeadersVisualStyles = false;
			this.uxIspisKolonaDataGridView.DataError += new System.Windows.Forms.DataGridViewDataErrorEventHandler(this.OnIspisKolonaDataGridViewDataError);
			this.uxIspisKolonaDataGridView.CellValueNeeded += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.OnIspisKolonaDataGridViewCellValueNeeded);
			this.uxIspisKolonaDataGridView.CellValuePushed += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.OnIspisKolonaDataGridViewCellValuePushed);
			
			//
			// uxIDDataGridViewColumn
			//
			this.uxIDDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxIDDataGridViewColumn.DataPropertyName = "ID";
			this.uxIDDataGridViewColumn.HeaderText = "ID";
			this.uxIDDataGridViewColumn.Name = "uxIDDataGridViewColumn";
			this.uxIDDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxIDDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxIDDataGridViewColumn.ReadOnly = true;		
			//
			// uxNazivDataGridViewColumn
			//
			this.uxNazivDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxNazivDataGridViewColumn.DataPropertyName = "Naziv";
			this.uxNazivDataGridViewColumn.HeaderText = "Naziv";
			this.uxNazivDataGridViewColumn.Name = "uxNazivDataGridViewColumn";
			this.uxNazivDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxNazivDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxNazivDataGridViewColumn.ReadOnly = false;		
			//
			// uxNaziv2DataGridViewColumn
			//
			this.uxNaziv2DataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxNaziv2DataGridViewColumn.DataPropertyName = "Naziv2";
			this.uxNaziv2DataGridViewColumn.HeaderText = "Naziv2";
			this.uxNaziv2DataGridViewColumn.Name = "uxNaziv2DataGridViewColumn";
			this.uxNaziv2DataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxNaziv2DataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxNaziv2DataGridViewColumn.ReadOnly = false;		
			//
			// uxIDIspisDataGridViewColumn
			//
			this.uxIDIspisDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxIDIspisDataGridViewColumn.DataPropertyName = "IDIspis";
			this.uxIDIspisDataGridViewColumn.HeaderText = "IDIspis";
			this.uxIDIspisDataGridViewColumn.Name = "uxIDIspisDataGridViewColumn";
			this.uxIDIspisDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxIDIspisDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxIDIspisDataGridViewColumn.ReadOnly = false;		
			//
			// uxIDIspisDataGridViewColumn
			//				
			this.uxIDIspisDataGridViewColumn.DisplayMember = "Naziv";	
			this.uxIDIspisDataGridViewColumn.ValueMember = "ID";	
			this.uxIDIspisDataGridViewColumn.DisplayStyleForCurrentCellOnly = true;
			//uxIDIspisDataGridViewColumn.DataSource = uxIDIspisBindingSource;				
				
			
			// this
			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.Controls.Add(this.uxIspisKolonaDataGridView);
			this.Name = "IspisKolonaDataGridView";
			this.Size = new System.Drawing.Size(470, 300);
			//((System.ComponentModel.ISupportInitialize)(this.uxIDIspisBindingSource)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxIspisKolonaErrorProvider)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxIspisKolonaDataGridView)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxIspisKolonaBindingSource)).EndInit();
			this.ResumeLayout(false);
		}
		#endregion
				
		#region events
		
		/// <summary>
		/// Raised the CurrentEntityChanged event.
		/// </summary>
		protected void OnCurrentEntityChanged()
		{
			if (CurrentEntityChanged != null)
			{
				IspisKolonaDataGridViewEventArgs args = new IspisKolonaDataGridViewEventArgs();
				args.IspisKolona = _currentIspisKolona;
				CurrentEntityChanged(this, args); 
			}
		}
		
		#endregion
		
		/// <summary>
		/// Strongly typed event 
		/// </summary>
		public class IspisKolonaDataGridViewEventArgs : System.EventArgs
		{
			private Entities.IspisKolona	_IspisKolona;
	
			/// <summary>
			/// the  Entities.IspisKolona instance.
			/// </summary>
			public Entities.IspisKolona IspisKolona
			{
				get { return _IspisKolona; }
				set { _IspisKolona = value; }
			}	
		}
		
		/// <summary>
		/// Handles the DataError event of the uxIspisKolonaDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewDataErrorEventArgs"/> instance containing the event data.</param>
		protected void OnIspisKolonaDataGridViewDataError(object sender, System.Windows.Forms.DataGridViewDataErrorEventArgs e)
		{
			System.Diagnostics.Debug.WriteLine(string.Format( "{0}, {1}", e.ColumnIndex, e.RowIndex));
		}
		
		/// <summary>
		/// Handles the CellValueNeeded event of the uxIspisKolonaDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewCellValueEventArgs"/> instance containing the event data.</param>
		protected void OnIspisKolonaDataGridViewCellValueNeeded(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)
		{
			switch(uxIspisKolonaDataGridView.Columns[e.ColumnIndex].Name)
			{
					case "uxIDDataGridViewColumn":
						e.Value = IspisKolonaList[e.RowIndex].ID;
						break;
					case "uxNazivDataGridViewColumn":
						e.Value = IspisKolonaList[e.RowIndex].Naziv;
						break;
					case "uxNaziv2DataGridViewColumn":
						e.Value = IspisKolonaList[e.RowIndex].Naziv2;
						break;
					case "uxIDIspisDataGridViewColumn":
						e.Value = IspisKolonaList[e.RowIndex].IDIspis;
						break;
				default:
				break;
			}
		}
		
		/// <summary>
		/// Handles the CellValuePushed event of the uxIspisKolonaDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewCellValueEventArgs"/> instance containing the event data.</param>
		protected void OnIspisKolonaDataGridViewCellValuePushed(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)
		{
			switch(uxIspisKolonaDataGridView.Columns[e.ColumnIndex].Name)
			{
					case "uxIDDataGridViewColumn":
						IspisKolonaList[e.RowIndex].ID = (System.Int32)e.Value;
						break;
					case "uxNazivDataGridViewColumn":
						IspisKolonaList[e.RowIndex].Naziv = (System.String)e.Value;
						break;
					case "uxNaziv2DataGridViewColumn":
						IspisKolonaList[e.RowIndex].Naziv2 = (System.String)e.Value;
						break;
					case "uxIDIspisDataGridViewColumn":
						IspisKolonaList[e.RowIndex].IDIspis = (System.Int32)e.Value;
						break;
				default:
				break;
			}
		}
	}
}
